package com.de3;

import java.util.Arrays;

public class Chess {

    char[][] board;
    char tile;

    public Chess(int size,int dr,int dc) {
        this.board = new char[size][size];
        tile = 'a';
        //特殊棋自子用 * 表示
        this.board[dr][dc] = '*';
    }

    public void chessBoard(int tr, int tc, int dr, int dc, int size){
        if (size == 1)return;
        int t = tile++;  //L型骨牌号
        int s = size / 2; //分割棋盘

        //覆盖左上角子棋盘
        if (dr < tr + s && dc < tc + s){
            //特殊棋盘在此
            chessBoard(tr,tc,dr,dc,s);
        }else {//此棋盘中午特殊方格
            //用t号L型骨牌覆盖左上角
            board[tr+s-1][tc+s-1] = (char)t;
           //覆盖其余方格
            chessBoard(tr,tc,tr+s-1,tc+s-1,s);
        }

        //覆盖右上角子棋盘
        if (dr<tr+s && dc >= tc+s){
            chessBoard(tr,tc+s,dr,dc,s);
        }else {
            board[tr+s-1][tc+s] = (char) t;
            chessBoard(tr,tc+s,tr+s-1,tc+s,s);
        }

        //覆盖左下角子棋盘
        if (dr >= tr+s && dc < tc+s){
            chessBoard(tr+s,tc,dr,dc,s);
        }else {
            board[tr+s][tc+s-1] = (char)t;
            chessBoard(tr+s,tc,tr+s,tc+s-1,s);
        }

        //覆盖右下角子棋盘
        if (dr >= tr+s && dc >= tc+s){
            chessBoard(tr+s,tc+s,dr,dc,s);
        }else {
            board[tr+s][tc+s] = (char)t;
            chessBoard(tr+s,tc+s,tr+s,tc+s,s);
        }
    }

    public static void main(String[] args) {
        int size = 8;
        int[][] board = new int[size][size];

        // 随机选择一个方格缺失
        int dr = (int) (Math.random() * size);
        int dc = (int) (Math.random() * size);

        // 执行棋盘覆盖算法
        Chess chess = new Chess(size,dr,dc);
        chess.chessBoard(0,0,dr,dc,size);

        chess.print();
    }

    public void print(){
        // 输出结果
        for (char[] row : board) {
            System.out.println(Arrays.toString(row));
        }
    }
}
